#include "menu.c"
#include "Polynomi.c"

int paflag,pbflag;
Polynomial pa,pb;
static char *items[]={"Create Pa","Create Pb","Pa+Pb","Pa-Pb","Pa*Pb","Pa/Pb",
		      "Delete Pa","Delete Pb","Exit"};
MENU Menu;
VIEW View,PaView,PbView;
VIEW CreateView,ShowView;

void TheCreatePoly(Polynomial *p,int m)
{
 float tempcoef;
 int tempexpn;
 int i;
 NODE *q1,*q2;

 *p=MakeNode(0,-1);

 q2=*p;
 for(i=1;i<=m;i++)                   /*Input coef and expn increasingly */
 {
  View_clear(&CreateView);
  cprintf("No.%d(Total %d)Input Coef:",i,m);
  cscanf("%f",&tempcoef);
  View_clear(&CreateView);
  cprintf("No.%d(Total %d)Input Expn:",i,m);
  cscanf("%d",&tempexpn);

  q1=q2->link;
  q2->link=MakeNode(tempcoef,tempexpn);
  q2->link->link=q1;
 }
}

void Init()
{
 paflag=pbflag=0;
 pa=pb=NULL;
 hidecursor();
 View_init(&View, 4,3,72,4, DARKGRAY,YELLOW,DOUBLELINE);
 View_init(&CreateView, 20,10,50,3,BLUE,YELLOW,DOUBLELINE);
 View_init(&ShowView, 1,15,80,4,BLACK,YELLOW,NOBORDER);
 View_init(&PaView, 1,19,80,3,BLACK,YELLOW,NOBORDER);
 View_init(&PbView, 1,22,80,3,BLACK,YELLOW,NOBORDER);
 Menu_init(&Menu,4,8,1,9,items,GREEN,BLACK,SINGLELINE);
 View_open(&View);
 cprintf("         Polynomials' + - * /(written by ZHANG JUN)                           Please Input polynomials by increasing expns!");
 View_open(&PaView);
 cprintf("Pa doesn't exit!");
 View_open(&PbView);
 cprintf("Pb doesn't exit!");
}

void CreatePa()
{
  int m;

  View_open(&CreateView);
  if(paflag)
  {
	  cprintf("pa exists,Delete it first!");
	  getch();
  }
  else
  {
    showcursor();
    paflag=1;
    cprintf("Input m:");
    cscanf("%d",&m);
    TheCreatePoly(&pa,m);
    hidecursor();
  }
 View_clear(&PaView);
 cprintf("Pa:");
 Display(pa);
 View_close(&CreateView);
}

void CreatePb()
{
  int m;
  View_open(&CreateView);
  if(pbflag)
  {
	  cprintf("pb exists,Delete it first!");
	  getch();
  }
  else
  {
    showcursor();
    pbflag=1;
    cprintf("Input m:");
    cscanf("%d",&m);
    TheCreatePoly(&pb,m);
    hidecursor();
  }
 View_clear(&PbView);
 cprintf("Pb:");
 Display(pb);
 View_close(&CreateView);
}

void ShowResult(int flag)
{
  Polynomial pr,prr;

  View_open(&ShowView);
  if(paflag==0||pbflag==0)
  {
	  cprintf("Create Polynomias first!");
  }
  else
  {
  switch(flag)
	  {
	  case 0:
		  pr=AddPoly(pa,pb);
		  break;
	  case 1:
		  pr=SubPoly(pa,pb);
		  break;
	  case 2:
		  pr=MultiPoly(pa,pb);
		  break;
	  case 3:
		  pr=DividePoly(pa,pb,&prr);
		  break;
	  }
	  cprintf("Result:");
          Display(pr);
	  DestoryPoly(&pr);
	  if(flag==3)
	  {
		  getch();
		  View_clear(&ShowView);
		  cprintf("Rest:");
		  Display(prr);
		  DestoryPoly(&prr);
	  }
  }
  getch();
  View_close(&ShowView);
}

void DeletePa()
{
 paflag=0;
 DestoryPoly(&pa);
 View_clear(&PaView);
 cprintf("Pa doesn't exit!");
}

void DeletePb()
{
 pbflag=0;
 DestoryPoly(&pb);
 View_clear(&PbView);
 cprintf("Pb doesn't exit!");
}

void main()
{
  int id,quit=0;

  Init();
  do
  {
	  id=Menu_drive(&Menu);
	  switch(id)
	  {
	  case 0:
		  CreatePa();
		  break;
	  case 1:
		  CreatePb();
		  break;
	  case 2:
		  ShowResult(0);
		  break;
	  case 3:
		  ShowResult(1);
		  break;
	  case 4:
		  ShowResult(2);
		  break;
	  case 5:
		  ShowResult(3);
		  break;
	  case 6:
		  DeletePa();
		  break;
	  case 7:
		  DeletePb();
		  break;
	  case 8:
		  quit=1;
		  break;
	  }
  }while(quit==0);

  View_close(&View);
  View_close(&PaView);
  View_close(&PbView);
  Menu_close(&Menu);
  DestoryPoly(&pa);
  DestoryPoly(&pb);
}